#include "Spike.h"

Spike::Spike()
{
}

Spike::~Spike()
{

}

void Spike::initGameObjectWith(GamePlayScreen *_gameScreen, CCDictionary *_dict)
{
    PhysicObject::initGameObjectWith(_gameScreen, _dict);

    this->createSprite();
    this->createPhysicBody();
}

void Spike::createSprite()
{
    objType = SPIKE;
    sprite = new CCSprite();
    sprite->initWithSpriteFrameName("spike_0.png");
    sprite->setPosition(getPosFromDict(dataDict, sprite->getContentSize()));
    sprite->setTag(SPIKE);

    type = dataDict->valueForKey(KEY_TYPE)->intValue();
    switch (type) {
    case DIRECT_DOWN:
        sprite->setFlipY(true);
        break;
    case DIRECT_LEFT:
        sprite->setRotation(-90);
        break;
    case DIRECT_RIGHT:
        sprite->setRotation(90);
        break;

    default:
        break;
    }
}

void Spike::createPhysicBody()
{
    b2BodyDef bodyDef;
    bodyDef.type = b2_staticBody;

    bodyDef.userData = this;
    if (sprite) {
        bodyDef.position.Set(sprite->getPositionX() / PTM_RATIO, sprite->getPositionY() / PTM_RATIO);
    }
    physicBody = handler->world->CreateBody(&bodyDef);

    if (type == DIRECT_UP || type == DIRECT_DOWN) {
        DataAccess::sharedDataAccess()->getFixturesForBody(physicBody, "spike1", 0);
    }
    else if (type == DIRECT_LEFT || type == DIRECT_RIGHT) {
        DataAccess::sharedDataAccess()->getFixturesForBody(physicBody, "spike2", 0);
    }
}
